package com.bardsoftware.foronuvolo.server;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.bardsoftware.foronuvolo.data.CollectionsUtil;
import com.bardsoftware.foronuvolo.data.Discussion;
import com.bardsoftware.foronuvolo.data.AnswersViewItem;
import com.bardsoftware.foronuvolo.data.ForumUser;
import com.bardsoftware.foronuvolo.data.DiscussionsViewItem;
import com.bardsoftware.foronuvolo.data.Message;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.repackaged.com.google.common.base.Pair;
import com.google.appengine.repackaged.com.google.common.collect.Lists;


public class AnswersViewServlet extends HttpServlet  {
    private final MessageCache<Key,Pair<String,Boolean>> myQuestionTextCache;
    private final MessageCache<Key,String> myAnswerTextCache;

    private final WikiMessageFormatter myFormatter = new WikiMessageFormatter();

    public AnswersViewServlet() {
        Cache cache;
        try {
            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        } catch (CacheException e) {
            e.printStackTrace();
            cache = null;
        }
        myQuestionTextCache = new MessageCache<Key, Pair<String,Boolean>>(cache, "f_");
        myAnswerTextCache = new MessageCache<Key, String>(cache, "fa_");
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        if (!req.getRequestURL().toString().startsWith(ForoNuvoloConstants.FORUM_DOMAIN)) {
            return;
        }
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(JspHelper.getJspName("answers_view.jsp"));
        String creationID = DiscussionsViewItem.getRefIDFromViewPath(req.getRequestURI());
        Discussion d = Discussion.findByRefID(creationID);
        if (d == null) {
            return;
        }
        req.setAttribute("discussion", new DiscussionsViewItem(d, myQuestionTextCache, myFormatter));
        req.setAttribute("messages", getDiscussionViewItems(d));
        ForumUser user = com.bardsoftware.foronuvolo.server.UserService.getUser(req);
        req.setAttribute("nickname", user.getNickname());
        if (user != ForumUser.ANONYMOUS) {
            req.setAttribute("user_id", user.getID());
        }
        req.setAttribute("redirectUrl", req.getRequestURI());
        req.setAttribute("friendConnectID", ForoNuvoloConstants.FRIEND_CONNECT_SITE_ID);
        if (req.getHeader("User-Agent").contains("Opera")) {
            req.setAttribute("opera", "true");
        }

        dispatcher.forward(req, resp);
    }

    private List<AnswersViewItem> getDiscussionViewItems(Discussion d) {
        List<AnswersViewItem> result = Lists.newArrayListWithExpectedSize(d.getSortedMessages().size());
        for (Message m: (d.getSortedMessages().subList(1, d.getSortedMessages().size()))) {
            result.add(new AnswersViewItem(m, myAnswerTextCache));
        }
        return result;
    }
}
